package com.wing.order.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wing.order.dao.OrderDeliveryDetailDao;
import com.wing.order.model.entity.OrderDeliveryDetail;
import com.wing.order.model.entity.OrderDeliverySingnum;
import com.wing.order.model.request.OrderDeliveryDetailForm;
import com.wing.order.model.response.OrderDeliveryDetailVO;
import com.wing.order.model.response.OrderDeliverySingnumVO;
import com.wing.order.model.response.ScanDeliveryVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 配送明细-ServiceImpl
 *
 * @author
 */
@Service
@Slf4j
public class OrderDeliveryDetailServiceImpl extends ServiceImpl<OrderDeliveryDetailDao, OrderDeliveryDetail> implements OrderDeliveryDetailService {

    @Autowired
    private OrderDeliverySingnumService orderDeliverySingnumService;

    @Override
    public int add(OrderDeliveryDetailForm orderDeliveryDetailForm) {

        //校验

        OrderDeliveryDetail orderDeliveryDetail = new OrderDeliveryDetail();
        CopyOptions copyOptions = CopyOptions.create()
                .setEditable(OrderDeliveryDetail.class)
                .setIgnoreError(true)
                .setIgnoreNullValue(true);
        BeanUtil.copyProperties(orderDeliveryDetailForm, orderDeliveryDetail, copyOptions);

        return baseMapper.insert(orderDeliveryDetail);
    }

    @Override
    public List<OrderDeliveryDetailVO> list4app(Map<String, Object> params) {
        return baseMapper.list4app(params);
    }

    @Override
    public Page<OrderDeliveryDetailVO> pageList4app(Page page, Map<String, Object> params) {
        List<OrderDeliveryDetailVO> list = baseMapper.list4app(page, params);
        page.setRecords(list);
        return page;
    }

    @Override
    public OrderDeliveryDetailVO get4appById(Long id) {
        return baseMapper.get4appById(id);
    }

    @Override
    public List<OrderDeliveryDetailVO> signDetail(String skuCode) {
        List<OrderDeliveryDetailVO> list = baseMapper.signDetail(skuCode);
        //up签收数量
        Integer[] arr = new Integer[list.size()];
        for (int i = 0; i < list.size(); i++) {
            arr[i] = list.get(i).getNum();
        }
        System.out.println(Arrays.toString(arr));
        int temp = 0;
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr.length - i - 1; j++) {
                temp = arr[j + 1] - arr[j];
            }
            list.get(i).setNum(temp);
            System.out.println(list.get(i).getNum() + "&");
        }
        return list;
    }


    @Override
    public ScanDeliveryVo scanPickupInfo(String packLabelCode, String deliveryOrderCode) {
        return baseMapper.scanPickupInfo(packLabelCode,deliveryOrderCode);
    }

    @Override
    public Integer countNotFinish(Long deliveryDetailId) {
        return baseMapper.countNotFinish(deliveryDetailId);
    }




}

